﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using UserControlLib;
using UserControlLib.Models;

namespace Wpf_Examples.ViewModels
{
    public class RadarViewModel:ObservableObject
    {
        #region 雷达数据属性
        /// <summary>
        /// 雷达
        /// </summary>
        private ObservableCollection<RadarModel> radarList;
        public ObservableCollection<RadarModel> RadarList
        {
            get { return radarList; }
            set { SetProperty(ref radarList, value); }
        }
        #endregion
        public RadarViewModel() {

            #region 初始化雷达数据 
            RadarList = new ObservableCollection<RadarModel>();
            RadarList.Add(new RadarModel { ItemName = "闪避", Value = 90 });
            RadarList.Add(new RadarModel { ItemName = "防御", Value = 30.00 });
            RadarList.Add(new RadarModel { ItemName = "暴击", Value = 34.89 });
            RadarList.Add(new RadarModel { ItemName = "攻击", Value = 69.59 });
            RadarList.Add(new RadarModel { ItemName = "速度", Value = 20 });
            CreateTimer(); //创建定时器动态改变数据
            #endregion
        }

        private void CreateTimer()
        {
            #region 每秒定时器服务
            DispatcherTimer cpuTimer = new DispatcherTimer
            {
                Interval = new TimeSpan(0, 0, 0, 3, 0)
            };
            cpuTimer.Tick += DispatcherTimer_Tick;
            cpuTimer.Start();
            #endregion
        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            Random random = new Random();
            foreach (var item in RadarList) // 使用 ToList 创建副本，避免在迭代过程中修改集合
            {
                App.Current.Dispatcher.Invoke(() =>
                {
                    item.Value = random.Next(20, 100); // 假设 Value 是一个介于 20 到 100 之间的整数
                });
            }
        }
    }
}
